<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\helpers\ActionButton;
use backend\helpers\FileViewHelper;
use backend\models\Decree;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\DecreeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Приказы';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container decree-index">

    <h1 class="main-title"><?= Html::encode($this->title) ?></h1>

    <p>
        <?= ActionButton::create('Добавить приказ', 'createDecree') ?>
        <?= ActionButton::SimpleLink('Типы приказов', 'decree-type/index', 'browseDecreeType') ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'tableOptions' => [
            'class' => 'table table-striped'
        ],
        'columns' => [
            'own_number',
            'title',
            [
                'attribute' => 'date_signature',
                'filter' => Html::activeInput('date', $searchModel, 'date_signature', ['class' => 'form-control']),
                'format' => 'date',
            ],
            [
                'attribute' => 'date_before',
                'filter' => Html::activeInput('date', $searchModel, 'date_before', ['class' => 'form-control']),
                'value' => function ($model) {
                    if($model->date_before && $model->date_before>date('Y-m-d'))
                    {
                        return '<span class="badge badge-success">' . \Yii::$app->formatter->asDate($model->date_before) . '</span>';
                    }
                    elseif($model->date_before && $model->date_before<date('Y-m-d'))
                    {
                        return '<span class="badge badge-secondary">' . \Yii::$app->formatter->asDate($model->date_before) . '</span>';
                    }
                    {
                        return '<span class="badge badge-success">бессрочно</span>';
                    }
                },
                'format' => 'raw',
            ],
            [
                'attribute' => 'id_type',
                'filter' => $decreeTypeList,
                'filterInputOptions' => ['class' => 'form-control form-control-sm'],
                'contentOptions' => ['style' => 'width:200px; white-space: normal;'],
                'value' => 'type.title',
            ],
            [
                'attribute'=>'file_name',
                'value' => function ($model) {
                    return FileViewHelper::simple(Decree::BUCKET_NAME, $model->file_name);
                },
                'format' => 'raw'
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'visibleButtons' =>
                [
                    'view' => Yii::$app->user->can('browseDecree'),
                    'update' => Yii::$app->user->can('updateDecree'),
                    'delete' => Yii::$app->user->can('deleteDecree'),
                ]
            ],
        ],
    ]); ?>


</div>
